package web;

import pojo.Book;
import pojo.Page;
import service.BookService;
import service.impl.BookServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BookServlet extends BaseServlet {
    private BookService book = new BookServiceImpl();
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //+1保证跳转之后 可以看到添加的图书
        String pageNo = req.getParameter("pageNo")+1;
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Book param = WebUtils.getParam(parameterMap, new Book());
        book.addBook(param);
        //这里如果用转发 则会有BUG 应该转发是一次请求,一直刷新页面会一直添加数据,所以要用重定向
//        req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
        //重定向/表示到端口号  所以要加个工程名
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //接受正在删除的数据的页数
        String pageNo = req.getParameter("pageNo");
        String id = req.getParameter("id");
        System.out.println(id);
        book.deleteBookyById(Integer.parseInt(id));
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Book param = WebUtils.getParam(parameterMap, new Book());
        book.updateBook(param);
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }
    //通过这个查询所有图书
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = book.queryAllBook();
        req.setAttribute("book",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取请求的参数图书编号
        String id = req.getParameter("id");
        //调用查询方法
        Book book = this.book.queryBookById(Integer.parseInt(id));
        //将查询得到的book对象设置到Request域中
        req.setAttribute("book",book);
        //请求转发到修改页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分页查询
        //页数
        int pageNo = WebUtils.paraseInt(req.getParameter("pageNo"),1);
        //一页多少内容
        int pageSize = WebUtils.paraseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //要传进去的servlet程序的url
        String url = "manager/bookServlet?action=page";
        //获得图书信息
        Page<Book> page = book.page(pageNo,pageSize,url);
        System.out.println(page);
        //将图书信息保存到request域中
        req.setAttribute("book",page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

}
